package org.dea;

import java.io.File;
import java.util.Arrays;
import java.util.Map;

import org.dea.deploy.LocalhostDeploy;
import org.dea.deploy.ProjectDeploy;
import org.dea.facade.AllFacadesMaker;
import org.dea.facade.DEAFacades;
import org.dea.facade.RMIFacadeMaker;
import org.dea.facade.RMIInterfaceFacadeMaker;
import org.dea.facade.SwitchFacadeMaker;
import org.dea.mapping.DistributedComponent;
import org.dea.util.GenericCommandLineExecutor;
import org.dea.util.ProcessOutput;

import de.gulden.util.javasource.Class;

public class DistributedEasyAcceptSSHProjectDeployImpl implements DistributedEasyAccept {

	public ProcessOutput runDistributedEasyAccept( String projectPath, String xmlMappingFile,
			String classPath, DEAFacades facades, String easyAcceptScript)
			throws Exception {

		ProjectDeploy deployMan = new ProjectDeploy();
		try {
			ProcessOutput[] deployOut = deployMan.deployAll(projectPath,classPath,
					xmlMappingFile, facades);
			String[] cmd = { "java", "-cp", classPath, "easyaccept.EasyAccept",
					facades.getSwitchFacade(), easyAcceptScript };
			GenericCommandLineExecutor exec = new GenericCommandLineExecutor(
					cmd);
			exec.runCommand();
			exec.waitFor();
			ProcessOutput output = new ProcessOutput(Arrays.toString(cmd), exec
					.getStdOutput(), exec.getErrOutput());
			deployMan.shutdownAll(xmlMappingFile);
			return output;
		} catch (Exception e) {
			deployMan.shutdownAll(xmlMappingFile);
			throw e;
		}
	}

	public DEAFacades generateFacades(String sourceDir,
			String facadeFullQualifiedName, String xmlMapppingFile)
			throws Exception {
		File source = new File(sourceDir);
		Class rmiFacadeClass = new RMIFacadeMaker().generateFacade(source,
				facadeFullQualifiedName);
		Class rmiInterfaceClass = new RMIInterfaceFacadeMaker().generateFacade(
				source, facadeFullQualifiedName);
		Class switchFacadeClass = new SwitchFacadeMaker(xmlMapppingFile)
				.generateFacade(source, facadeFullQualifiedName);
		if (rmiFacadeClass.getPackage().getName().trim().equals("")) {
			String rmiFacade = rmiFacadeClass.getName();
			String rmiInterface = rmiInterfaceClass.getName();
			String switchFacade = switchFacadeClass.getName();
			return new DEAFacades(facadeFullQualifiedName, rmiInterface,
					rmiFacade, switchFacade);
		} else {
			String rmiFacade = rmiFacadeClass.getPackage().getName() + "."
					+ rmiFacadeClass.getUnqualifiedName();
			String rmiInterface = rmiInterfaceClass.getPackage().getName()
					+ "." + rmiInterfaceClass.getUnqualifiedName();
			String switchFacade = switchFacadeClass.getPackage().getName()
					+ "." + switchFacadeClass.getUnqualifiedName();
			return new DEAFacades(facadeFullQualifiedName, rmiInterface,
					rmiFacade, switchFacade);
		}

	}

	public static void main(String[] args) throws Exception {

		System.out.println("test/acceptTests/sumUS.txt");

		String xmlMappingFile = "test/xml/mappingSum.xml";
		String facadeFullQualifiedName = "somepack.innerpack.Sum";
		String sourceDir = "test";
		String classPath = "bin:lib/easyaccept.jar";
		String easyAcceptScript = "test/acceptTests/sumUS.txt";

		DistributedEasyAcceptSSHProjectDeployImpl dea = new DistributedEasyAcceptSSHProjectDeployImpl();
		DEAFacades facades = dea.generateFacades(sourceDir,
				facadeFullQualifiedName, xmlMappingFile);

		// TODO THIS will break if u do not compile, to compile, press refresh
		// ont test directory on package explorer!

		ProcessOutput po = dea.runDistributedEasyAccept(".",xmlMappingFile,
				classPath, facades, easyAcceptScript);
		System.out.println(po.getCmd());
		System.out.println(po.getStd());
		System.out.println(po.getErr());

		// another test
		System.out.println("test/acceptTests/pingUS.txt");

		xmlMappingFile = "test/xml/mappingPing.xml";
		facadeFullQualifiedName = "somepack.innerpack.Ping";
		sourceDir = "test";
		classPath = "bin:lib/easyaccept.jar";
		easyAcceptScript = "test/acceptTests/pingUS.txt";

		dea = new DistributedEasyAcceptSSHProjectDeployImpl();
		facades = dea.generateFacades(sourceDir, facadeFullQualifiedName,
				xmlMappingFile);

		// TODO THIS will break if u do not compile, to compile, press refresh
		// ont test directory on package explorer!

		po = dea.runDistributedEasyAccept(".",xmlMappingFile, classPath, facades,
				easyAcceptScript);
		System.out.println(po.getCmd());
		System.out.println(po.getStd());
		System.out.println(po.getErr());
	}

}
